package com.maxd.respository.selfGoods;


import com.maxd.model.Goods;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

public interface GoodsRepository extends Repository<Goods, Long> {

    @Query(value = "select * from goods " +
            "where if(:title!='',title like concat('%',:title,'%'),1=1) " +
            "and if(:type!='',type_id =:type,1=1) " +
            "and if(:storeId!='-1',store_id =:storeId,1=1) " +
            "and if(:status!=0,status=:status,1=1) " +
            "and if(:activityId!='',(activity_ids like concat('%,',:activityId,'%') or activity_ids like concat('%,',:activityId,',%') or activity_ids like concat('%',:activityId,',%') or activity_ids =:activityId),1=1) " +
            "and if(:notActivityId!='',((activity_ids not like concat('%,',:notActivityId,'%') and  activity_ids not like concat('%,',:notActivityId,',%') and  activity_ids not like concat('%',:notActivityId,',%') and activity_ids <>:notActivityId) or activity_ids is null),1=1) " +
            "and if(:isJiFenGoods ='1',is_ji_fen_goods=:isJiFenGoods,1=1) and if(:isJiFenGoods ='0',is_ji_fen_goods is null,1=1) order by create_at desc ",
            countQuery = "select count(*) from goods " +
                    "where if(:title!='',title like concat('%',:title,'%'),1=1) " +
                    "and if(:type!='',type_id =:type,1=1) " +
                    "and if(:storeId!='-1',store_id =:storeId,1=1) " +
                    "and if(:status!=0,status=:status,1=1) " +
                    "and if(:activityId!='',(activity_ids like concat('%,',:activityId,'%') or activity_ids like concat('%,',:activityId,',%') or activity_ids like concat('%',:activityId,',%') or activity_ids =:activityId),1=1) " +
                    "and if(:notActivityId!='',((activity_ids not like concat('%,',:notActivityId,'%') and  activity_ids not like concat('%,',:notActivityId,',%') and  activity_ids not like concat('%',:notActivityId,',%') and activity_ids <>:notActivityId) or activity_ids is null),1=1) " +
                    "and if(:isJiFenGoods ='1',is_ji_fen_goods=:isJiFenGoods,1=1) and if(:isJiFenGoods ='0',is_ji_fen_goods is null,1=1) ",nativeQuery = true)
    Page<Goods> findAll(Pageable pageable, String title, String type, String status,String storeId, String isJiFenGoods, String activityId, String notActivityId);


}